package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManagerService {
    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private MovementApi movementApi;
    public PageResult findAllUsers(Integer page, Integer pagesize) {
        //1.调用API分页查询数据列表 IPage<UserInfo>
        IPage<UserInfo> iPage = userInfoApi.findAll(page, pagesize);
        //2.需要将IPage转化为PageResult
        return new PageResult(page, pagesize, iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 根据id查询用户详情
     * @param userId
     * @return
     */
    public UserInfo findUserId(Long userId) {
        return userInfoApi.findById(userId);
    }

    public PageResult finAllVideos(Integer page, Integer pagesize, Long uid) {
        return videoApi.findAllVideos(page,pagesize,uid);
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        //1.调用API查询数据：Movement对象
        PageResult pr = movementApi.findByUserId(uid,state,page,pagesize);
        //2.解析PageResult，获取Movement对象列表
        List<Movement> items = (List<Movement>) pr.getItems();
        //3.一个Movement对象转化一个vo
        if(CollUtil.isEmpty(items)){
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = map.get(movement.getUserId());
            if (userInfo != null){
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                vos.add(vo);
            }
        }
        //构造返回值
        pr.setItems(vos);
        return pr;
    }

    //用户冻结
    public Map userFreeze(Map params) {
        Integer freezingTime = (Integer) params.get("freezingTime");
        Long userId = (Long) params.get("userId");
        int days = 0;
        if (freezingTime == 1){
            days = 3;
        }
        if (freezingTime == 2){
            days = 7;
        }
        if (freezingTime == 3){
            days = -1;
        }
        String value = JSON.toJSONString(params);
        redisTemplate.opsForValue().set(Constants.FREEZE_USER + userId,value,days, TimeUnit.DAYS);
        Map map = new HashMap();
        map.put("message","冻结成功");
        return map;
    }

    //用户解冻
    public Map unfreeze(Map params) {
        Long userId = (Long) params.get("userId");
        redisTemplate.delete(Constants.FREEZE_USER + userId);
        Map map = new HashMap();
        map.put("message","解冻成功");
        return map;
    }
}
